package com.upf1sh.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSONObject;
import com.upf1sh.bean.ChickenFarmInfo;
import com.upf1sh.bean.Robot;
import com.upf1sh.service.TopicOneService;
import com.upf1sh.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * UpF1sh
 * 2023/4/13 21:25
 * 课题一Controller
 */


@RestController
@RequestMapping("/topicOne")
public class TopicOneController {
    @Resource
    public TopicOneService topicOneService;
    @PostMapping(value = "/add", produces = "application/json;charset=UTF-8")

    public int insert(@RequestBody JSONObject robot) {
        Robot robot1 = JSONObject.parseObject(robot.toJSONString(), Robot.class);
        return topicOneService.insert_one(robot1) == 1 ? 1 : 0;
    }

    //    produces = "application/json;charset=UTF-8"
    @PostMapping(value = "/addByString")
    public int insertByString(@RequestBody String data) {
        return topicOneService.saveRobotData(data) == 1 ? 1 : 0;
    }

    //写一个接收图片的controller，使用post
    @PostMapping(value = "/addImg", produces = "application/json;charset=UTF-8")
    public int insertImg(@RequestBody JSONObject robot) {
        Robot robot1 = JSONObject.parseObject(robot.toJSONString(), Robot.class);
        return topicOneService.insert_one(robot1) == 1 ? 1 : 0;
    }

    @PostMapping("/test")
    public void test(@RequestBody String requestData) {
        System.out.println("pytest" + requestData);
//        System.out.println(jsonObject.toString());
//        return Result.ok();
    }

    /**
     * 查询全部
     * UpF1sh
     * 2023/4/24 10:49
     *
     * @return
     */
    @RequestMapping("/getAll")
    public Result getAll() {
        return Result.ok().data("robotStatus", topicOneService.getAllRobot());
    }

    @RequestMapping("/getData")
    public Result getData() {
        return Result.ok().data("data", topicOneService.getFileData());
    }

    @GetMapping("/export")
    public void exportCsv(HttpServletResponse response) throws IOException
    {

        List<Robot> allRobot = topicOneService.getAllRobot();

        // 设置响应头
        response.setContentType("text/xls;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("数据.csv", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName);
        EasyExcel.write(response.getOutputStream(), Robot.class)
                .excelType(ExcelTypeEnum.XLS)
                .sheet("数据")
                .doWrite(allRobot);
    }



    /**
     * 导入excel文件到数据库
     * 2023/4/23 22:12
     *
     * @param file
     * @return
     */
//    @PostMapping("/import")
//    public boolean addRobotStatus(@RequestParam("file") MultipartFile file) {
//        boolean a = false;
//        String fileName = file.getOriginalFilename();
//        try {
//            a = topicOneService.batchImport(fileName, file);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return a;
//    }
//
//    /**
//     * 2023/5/7 17:12
//     * 导出excel
//     *
//     * @param response
//     * @throws IOException
//     */
//    @RequestMapping("/export")
//    public void exportExcel(HttpServletResponse response) throws IOException {
//        topicOneService.exportExcel(response);
//    }

}

